<template>
	<view class="u-page__item">
		<u-tabbar :value="tabIndex" @change="routePage" :fixed="true" :placeholder="true" zIndex="999"
			activeColor="#3CC51F" :safeAreaInsetBottom="true">
			<u-tabbar-item :text="item.text" :badge="item.badge" v-for="item in list" :key="item.index">
				<image class="u-page__item__slot-icon" slot="active-icon" :src="item.iconPath"></image>
				<image class="u-page__item__slot-icon" slot="inactive-icon" :src="item.selectedIconPath"></image>
			</u-tabbar-item>
		</u-tabbar>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	export default {
		name: "tab-bar",
		props: {
			// 导航栏背景色相关
			index: {
				type: String,
				default: "",
				require: false,
			},
		},
		data() {
			return {
				list: [{
					index: 0,
					page: '/pages/home/home',
					iconPath: "/static/image/index.png",
					selectedIconPath: "/static/image/index-selected.png",
					badge: 0,
					text: "首页"
				}, {
					index: 1,
					page: '/pages/classification/classification',
					iconPath: "/static/image/classification.png",
					selectedIconPath: "/static/image/classification-selected.png",
					badge: 0,
					text: "分类"
				}, {
					index: 2,
					page: '/pages/message/list/index',
					iconPath: "/static/image/message.png",
					selectedIconPath: "/static/image/message-selected.png",
					badge: 0,
					text: "消息"
				}, {
					index: 3,
					page: '/pages/cart/cart',
					iconPath: "/static/image/message.png",
					selectedIconPath: "/static/image/message-selected.png",
					badge: 0,
					text: "购物车"
				}, {
					index: 4,
					page: '/pages/member/index/index',
					iconPath: "/static/image/member.png",
					selectedIconPath: "/static/image/member-selected.png",
					badge: 0,
					text: "我的"
				}, ]
			};
		},
		computed: {
			...mapState('tabbar', ['tabIndex', 'messageBadge', 'cartBadge'])
		},
		watch: {
			messageBadge(value) {
				let message = this.list.find(item => item.index == 2);
				message.badge = value;
			},
			cartBadge(value) {
				let cart = this.list.find(item => item.index == 3);
				cart.badge = value;
			},

		},
		mounted() {
			const item = this.list.find(item => item.index == this.tabIndex);
			const currentRoute = this.$route.path;
			// 可能出现当前路由和tabbar对应不上情况
			if (currentRoute != item.page) {
				// uni.reLaunch({
				// 	url: item.page
				// });
			}
			// 更新徽标
			let message = this.list.find(item => item.index == 2);
			message.badge = this.messageBadge;
			let cart = this.list.find(item => item.index == 3);
			cart.badge = this.cartBadge;
		},
		methods: {
			...mapMutations('tabbar', ['updateTabIndex']),
			routePage(index) {
				//更新组件状态
				this.updateTabIndex(index);

				const item = this.list.find(item => item.index == index);
				uni.reLaunch({
					url: item.page
				});
			},
		}
	}
</script>

<style lang="scss">
	.u-page {
		padding: 0;
		margin-top: 200rpx;

		&__item {

			&__slot-icon {
				width: 48rpx;
				height: 48rpx;
			}
		}
	}

	.u-page__item {
		height: 102rpx;
	}
</style>